package com.java_foundation.thread_demo.test_8;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @program: java_foundation
 * @description: sleep方法示例说明
 * @author: xiongbangwen <Email>xiongbangwen@163.com</Email>
 * @create: 2020-06-21 17:56
 **/
public class SleepDemo {

    //创建一个独占锁
    private static final Lock lock = new ReentrantLock();

    public static void main(String[] args) {
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                //获取独占锁
                lock.lock();

                try {
                    System.out.println("线程A开始睡眠");
                    Thread.sleep(10000);
                    System.out.println("线程A睡眠结束");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                  //释放锁
                   lock.unlock();
                }
            }
        });
        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                //获取独占锁
                lock.lock();

                try {
                    System.out.println("线程B开始睡眠");
                    Thread.sleep(10000);
                    System.out.println("线程B睡眠结束");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    //释放锁
                    lock.unlock();
                }
            }
        });

        //启动线程
        threadA.start();
        threadB.start();
    }
}
